<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Node.js,Express,EJS,MySQL," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="&amp;#x8BE5;&amp;#x9879;&amp;#x76EE;&amp;#x662F;&amp;#x4EFF;&amp;#x7167;&amp;#x67D0;&amp;#x4E9B;&amp;#x7F51;&amp;#x7AD9;&amp;#x7684;&amp;#x7559;&amp;#x8A00;&amp;#x5217;&amp;#x8868;&amp;#x505A;&amp;#x7684;&amp;#x4E00;&amp;#x4E9B;&amp;#x5C0F;&amp;#x529F;&amp;#x80FD;&amp;#xFF0C;&amp;#x9879;&amp;#x76EE;">
<meta property="og:type" content="article">
<meta property="og:title" content="Node-留言列表小项目">
<meta property="og:url" content="https://hongqinma.github.io/2016/11/19/s014-Node-留言列表小项目/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="&amp;#x8BE5;&amp;#x9879;&amp;#x76EE;&amp;#x662F;&amp;#x4EFF;&amp;#x7167;&amp;#x67D0;&amp;#x4E9B;&amp;#x7F51;&amp;#x7AD9;&amp;#x7684;&amp;#x7559;&amp;#x8A00;&amp;#x5217;&amp;#x8868;&amp;#x505A;&amp;#x7684;&amp;#x4E00;&amp;#x4E9B;&amp;#x5C0F;&amp;#x529F;&amp;#x80FD;&amp;#xFF0C;&amp;#x9879;&amp;#x76EE;">
<meta property="og:updated_time" content="2016-12-04T02:27:11.538Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Node-留言列表小项目">
<meta name="twitter:description" content="&amp;#x8BE5;&amp;#x9879;&amp;#x76EE;&amp;#x662F;&amp;#x4EFF;&amp;#x7167;&amp;#x67D0;&amp;#x4E9B;&amp;#x7F51;&amp;#x7AD9;&amp;#x7684;&amp;#x7559;&amp;#x8A00;&amp;#x5217;&amp;#x8868;&amp;#x505A;&amp;#x7684;&amp;#x4E00;&amp;#x4E9B;&amp;#x5C0F;&amp;#x529F;&amp;#x80FD;&amp;#xFF0C;&amp;#x9879;&amp;#x76EE;">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2016/11/19/s014-Node-留言列表小项目/"/>


  <title> Node-留言列表小项目 | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Node-留言列表小项目
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2016-11-19T12:50:39+08:00" content="2016-11-19">
              2016-11-19
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Node-js/" itemprop="url" rel="index">
                    <span itemprop="name">Node.js</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/11/19/s014-Node-留言列表小项目/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/11/19/s014-Node-留言列表小项目/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#x8BE5;&#x9879;&#x76EE;&#x662F;&#x4EFF;&#x7167;&#x67D0;&#x4E9B;&#x7F51;&#x7AD9;&#x7684;&#x7559;&#x8A00;&#x5217;&#x8868;&#x505A;&#x7684;&#x4E00;&#x4E9B;&#x5C0F;&#x529F;&#x80FD;&#xFF0C;&#x9879;&#x76EE;&#x6CA1;&#x6709;&#x5212;&#x5206; M V C&#xFF0C;&#x4F7F;&#x7528;&#x7684;&#x524D;&#x53F0;&#x6280;&#x672F;&#x662F; Bootstrap&#xFF0C;&#x540E;&#x53F0;&#x4F7F;&#x7528;&#x7684;&#x662F; Node.js&#xFF0C;&#x6A21;&#x677F;&#x5F15;&#x64CE;&#x4F7F;&#x7528;&#x7684;&#x662F; EJS&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x662F; MySQL&#x3002;</p>
<h2 id="&#x4F9D;&#x8D56;"><a href="#&#x4F9D;&#x8D56;" class="headerlink" title="&#x4F9D;&#x8D56;"></a>&#x4F9D;&#x8D56;</h2><ul>
<li>Bootstrap</li>
<li>jQuery<ul>
<li>&#x4F7F;&#x7528; jQuery &#x7684; ajax &#x505A;&#x5F02;&#x6B65;&#x65E0;&#x5237;&#x65B0;&#x5217;&#x8868;&#x52A0;&#x8F7D;</li>
</ul>
</li>
<li>EJS</li>
<li>Node.js<ul>
<li>express<ul>
<li>body-parser express &#x7684;&#x4E2D;&#x95F4;&#x4EF6;(middleware)&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x89E3;&#x6790; post &#x8BF7;&#x6C42;&#x4F53;&#x6570;&#x636E;</li>
</ul>
</li>
<li>mysql</li>
<li>moment</li>
</ul>
</li>
<li>js-cookie</li>
</ul>
<h3 id="&#x5B89;&#x88C5;&#x4F9D;&#x8D56;"><a href="#&#x5B89;&#x88C5;&#x4F9D;&#x8D56;" class="headerlink" title="&#x5B89;&#x88C5;&#x4F9D;&#x8D56;"></a>&#x5B89;&#x88C5;&#x4F9D;&#x8D56;</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install bootstrap jquery express art-template --save</div></pre></td></tr></table></figure>
<h3 id="package-json-&#x5305;&#x62EC;&#x9879;&#x76EE;&#x540E;&#x9762;&#x6D41;&#x7A0B;&#x5B89;&#x88C5;&#x7684;&#x4F9D;&#x8D56;&#x5305;"><a href="#package-json-&#x5305;&#x62EC;&#x9879;&#x76EE;&#x540E;&#x9762;&#x6D41;&#x7A0B;&#x5B89;&#x88C5;&#x7684;&#x4F9D;&#x8D56;&#x5305;" class="headerlink" title="package.json (&#x5305;&#x62EC;&#x9879;&#x76EE;&#x540E;&#x9762;&#x6D41;&#x7A0B;&#x5B89;&#x88C5;&#x7684;&#x4F9D;&#x8D56;&#x5305;)"></a>package.json (&#x5305;&#x62EC;&#x9879;&#x76EE;&#x540E;&#x9762;&#x6D41;&#x7A0B;&#x5B89;&#x88C5;&#x7684;&#x4F9D;&#x8D56;&#x5305;)</h3><figure class="highlight json"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">{</div><div class="line">  <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;feedback&quot;</span>,</div><div class="line">  <span class="attr">&quot;version&quot;</span>: <span class="string">&quot;1.0.0&quot;</span>,</div><div class="line">  <span class="attr">&quot;description&quot;</span>: <span class="string">&quot;&quot;</span>,</div><div class="line">  <span class="attr">&quot;main&quot;</span>: <span class="string">&quot;index.js&quot;</span>,</div><div class="line">  <span class="attr">&quot;scripts&quot;</span>: {</div><div class="line">    <span class="attr">&quot;test&quot;</span>: <span class="string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span></div><div class="line">  },</div><div class="line">  <span class="attr">&quot;keywords&quot;</span>: [],</div><div class="line">  <span class="attr">&quot;author&quot;</span>: <span class="string">&quot;lpz &lt;mail@lipengzhou.com&gt; (http://www.lipengzhou.com/)&quot;</span>,</div><div class="line">  <span class="attr">&quot;license&quot;</span>: <span class="string">&quot;MIT&quot;</span>,</div><div class="line">  <span class="attr">&quot;dependencies&quot;</span>: {</div><div class="line">    <span class="attr">&quot;art-template&quot;</span>: <span class="string">&quot;^3.0.3&quot;</span>,</div><div class="line">    <span class="attr">&quot;body-parser&quot;</span>: <span class="string">&quot;^1.15.2&quot;</span>,</div><div class="line">    <span class="attr">&quot;bootstrap&quot;</span>: <span class="string">&quot;^3.3.7&quot;</span>,</div><div class="line">    <span class="attr">&quot;ejs&quot;</span>: <span class="string">&quot;^2.5.2&quot;</span>,</div><div class="line">    <span class="attr">&quot;express&quot;</span>: <span class="string">&quot;^4.14.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;jquery&quot;</span>: <span class="string">&quot;^3.1.1&quot;</span>,</div><div class="line">    <span class="attr">&quot;js-cookie&quot;</span>: <span class="string">&quot;^2.1.3&quot;</span>,</div><div class="line">    <span class="attr">&quot;moment&quot;</span>: <span class="string">&quot;^2.16.0&quot;</span>,</div><div class="line">    <span class="attr">&quot;mysql&quot;</span>: <span class="string">&quot;^2.12.0&quot;</span></div><div class="line">  }</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="&#x9875;&#x9762;&#x642D;&#x5EFA;&#xFF1A;Bootstrap"><a href="#&#x9875;&#x9762;&#x642D;&#x5EFA;&#xFF1A;Bootstrap" class="headerlink" title="&#x9875;&#x9762;&#x642D;&#x5EFA;&#xFF1A;Bootstrap"></a>&#x9875;&#x9762;&#x642D;&#x5EFA;&#xFF1A;Bootstrap</h2><blockquote>
<p>&#x6700;&#x7EC8;&#x5B8C;&#x6574;&#x9875;&#x9762;</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>&#x53CD;&#x9988;&#x7559;&#x8A00;&#x672C;<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/node_modules/bootstrap/dist/css/bootstrap.css&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- &#x9875;&#x5934; --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;page-header&quot;</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">h1</span>&gt;</span>&#x53CD;&#x9988;&#x7CFB;&#x7EDF;<span class="tag">&lt;<span class="name">small</span>&gt;</span>&#x7559;&#x8A00;&#x672C;<span class="tag">&lt;/<span class="name">small</span>&gt;</span><span class="tag">&lt;/<span class="name">h1</span>&gt;</span>&#x6B22;&#x8FCE;&#xFF1A;<span class="tag">&lt;<span class="name">%=</span> <span class="attr">user.username</span> %&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- &#x9875;&#x5934; --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- &#x7559;&#x8A00;&#x5217;&#x8868;&#x533A;&#x57DF; --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;list-group&quot;</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item list-group-item-info&quot;</span>&gt;</span>&#x7559;&#x8A00;&#xFF08;<span class="tag">&lt;<span class="name">span</span> <span class="attr">id</span>=<span class="string">&quot;message_count&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span>&#x6761;&#xFF09;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- /&#x7559;&#x8A00;&#x5217;&#x8868;&#x533A;&#x57DF; --&gt;</span></div><div class="line">  <span class="comment">&lt;!-- &#x7559;&#x8A00;&#x8868;&#x5355;&#x533A;&#x57DF; --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;panel panel-warning&quot;</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;panel-heading&quot;</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h3</span> <span class="attr">class</span>=<span class="string">&quot;panel-title&quot;</span>&gt;</span>&#x6211;&#x8981;&#x53D1;&#x8868;&#x770B;&#x6CD5;<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;panel-body&quot;</span>&gt;</span></div><div class="line">        <span class="comment">&lt;!-- &#x540E;&#x53F0;&#x6307;&#x5B9A;&#x63D0;&#x4EA4;&#x7684;&#x63A5;&#x53E3; --&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">form</span> <span class="attr">id</span>=<span class="string">&quot;form&quot;</span> <span class="attr">action</span>=<span class="string">&quot;/message/add&quot;</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span> <span class="attr">role</span>=<span class="string">&quot;form&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">div</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">label</span> <span class="attr">for</span>=<span class="string">&quot;&quot;</span>&gt;</span>&#x60A8;&#x7684;&#x7559;&#x8A00;<span class="tag">&lt;/<span class="name">label</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">textarea</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">required</span> <span class="attr">name</span>=<span class="string">&quot;message&quot;</span> <span class="attr">rows</span>=<span class="string">&quot;5&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">div</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">label</span> <span class="attr">for</span>=<span class="string">&quot;&quot;</span>&gt;</span>&#x60A8;&#x7684;&#x5927;&#x540D;<span class="tag">&lt;/<span class="name">label</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">required</span> <span class="attr">name</span>=<span class="string">&quot;name&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x60A8;&#x7684;&#x5927;&#x540D;&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;form-group&quot;</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">label</span> <span class="attr">for</span>=<span class="string">&quot;&quot;</span>&gt;</span>&#x7535;&#x5B50;&#x90AE;&#x4EF6;<span class="tag">&lt;/<span class="name">label</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;email&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">required</span> <span class="attr">name</span>=<span class="string">&quot;email&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x60A8;&#x7684;&#x7535;&#x5B50;&#x90AE;&#x4EF6;&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-success&quot;</span>&gt;</span>&#x53D1;&#x8868;<span class="tag">&lt;/<span class="name">button</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">form</span>&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="comment">&lt;!-- /&#x7559;&#x8A00;&#x8868;&#x5355;&#x533A;&#x57DF; --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/template&quot;</span> <span class="attr">id</span>=<span class="string">&quot;tpl&quot;</span>&gt;</span><span class="javascript"></span></div><div class="line">    {{each messages <span class="keyword">as</span> message index}}</div><div class="line">    &lt;li <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;list-group-item&quot;</span>&gt;{{message.name}}&#xFF1A;{{message.message}}&lt;span <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;pull-right&quot;</span>&gt;{{message.date}}&lt;<span class="regexp">/span&gt;&lt;/</span>li&gt;</div><div class="line">    {{/each}}</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;/node_modules/art-template/dist/template.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;/node_modules/jquery/dist/jquery.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></div><div class="line">    loadMessages();</div><div class="line">    <span class="function"><span class="keyword">function</span> <span class="title">loadMessages</span>(<span class="params"></span>) </span>{</div><div class="line">      <span class="comment">// ajax &#x5DF2;&#x6B7B;&#xFF0C; &#x672A;&#x6765; fetch &#x53EF;&#x80FD;&#x4F1A;&#x6210;&#x4E3A;&#x6807;&#x51C6;</span></div><div class="line">      <span class="built_in">window</span>.fetch(<span class="string">&apos;/message&apos;</span>);</div><div class="line">        .then(<span class="function"><span class="params">res</span> =&gt;</span> {</div><div class="line">          <span class="keyword">return</span> res.json();</div><div class="line">        })</div><div class="line">        .then(<span class="function"><span class="params">data</span> =&gt;</span> {</div><div class="line">          $(<span class="string">&apos;.list-group li:gt(0)&apos;</span>).remove();</div><div class="line">          <span class="comment">// &#x524D;&#x53F0;&#x6E32;&#x67D3;&#x9875;&#x9762;</span></div><div class="line">          <span class="keyword">const</span> result = template(<span class="string">&apos;tpl&apos;</span>, data);</div><div class="line">          $(<span class="string">&apos;#message_count&apos;</span>).html(data.messages.length);</div><div class="line">          $(<span class="string">&apos;.list-group&apos;</span>).append(result);</div><div class="line">        });</div><div class="line">    }</div><div class="line">    $(<span class="string">&apos;#form&apos;</span>).on(<span class="string">&apos;submit&apos;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>{</div><div class="line">      <span class="comment">// &#x52A0;&#x4E0A;&#x8FD9;&#x53E5;&#xFF0C;&#x8868;&#x5355;&#x4E0D;&#x4F1A;&#x9ED8;&#x8BA4;&#x63D0;&#x4EA4;&#x4E86;</span></div><div class="line">      e.preventDefault();</div><div class="line">      $.ajax({</div><div class="line">          <span class="attr">url</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;action&apos;</span>),</div><div class="line">          <span class="attr">type</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;method&apos;</span>),</div><div class="line">          <span class="attr">data</span>: $(<span class="keyword">this</span>).serialize(),</div><div class="line">          <span class="attr">dataType</span>: <span class="string">&apos;json&apos;</span></div><div class="line">        })</div><div class="line">        .then(<span class="function"><span class="params">data</span> =&gt;</span> {</div><div class="line">          <span class="comment">// &#x662F;&#x5426;&#x6210;&#x529F;&#x7531;&#x540E;&#x53F0;&#x544A;&#x8BC9;&#x524D;&#x53F0;</span></div><div class="line">          <span class="keyword">if</span> (data.code === <span class="number">1000</span>) {</div><div class="line">            <span class="comment">// &#x6210;&#x529F;&#x540E;&#x6E05;&#x7A7A;&#x7559;&#x8A00;&#x8F93;&#x5165;&#x6846;</span></div><div class="line">            $(<span class="string">&apos;#form [name]&apos;</span>).val(<span class="string">&apos;&apos;</span>);</div><div class="line">            loadMessages();</div><div class="line">            <span class="built_in">window</span>.alert(<span class="string">&apos;&#x7559;&#x8A00;&#x6210;&#x529F;&apos;</span>);</div><div class="line">          }</div><div class="line">        });</div><div class="line">       <span class="comment">// &#x548C; e.preventDefault(); &#x4F5C;&#x7528;&#x76F8;&#x540C;</span></div><div class="line">      <span class="comment">// return false;</span></div><div class="line">    });</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h2 id="&#x6570;&#x636E;&#x5E93;&#x8BBE;&#x8BA1;-feedback"><a href="#&#x6570;&#x636E;&#x5E93;&#x8BBE;&#x8BA1;-feedback" class="headerlink" title="&#x6570;&#x636E;&#x5E93;&#x8BBE;&#x8BA1;: feedback"></a>&#x6570;&#x636E;&#x5E93;&#x8BBE;&#x8BA1;: feedback</h2><table>
<thead>
<tr>
<th>&#x540D;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x957F;&#x5EA6;</th>
<th>&#x5C0F;&#x6570;&#x70B9;</th>
<th>&#x4E0D;&#x662F;null</th>
<th>&#x4E3B;&#x952E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>int</td>
<td></td>
<td></td>
<td>yes</td>
<td>yes</td>
</tr>
<tr>
<td>message</td>
<td>text</td>
<td></td>
<td></td>
<td>yes</td>
<td></td>
</tr>
<tr>
<td>name</td>
<td>varchar</td>
<td>50</td>
<td></td>
<td>yes</td>
<td></td>
</tr>
<tr>
<td>email</td>
<td>varchar</td>
<td>50</td>
<td></td>
<td>yes</td>
<td></td>
</tr>
<tr>
<td>data</td>
<td>datatime</td>
<td></td>
<td></td>
<td>yes</td>
</tr>
</tbody>
</table>
<h2 id="&#x9875;&#x9762;&#x5165;&#x53E3;-&#x540E;&#x53F0;&#x6570;&#x636E;&#x5904;&#x7406;-app-js"><a href="#&#x9875;&#x9762;&#x5165;&#x53E3;-&#x540E;&#x53F0;&#x6570;&#x636E;&#x5904;&#x7406;-app-js" class="headerlink" title="&#x9875;&#x9762;&#x5165;&#x53E3;/&#x540E;&#x53F0;&#x6570;&#x636E;&#x5904;&#x7406; app.js"></a>&#x9875;&#x9762;&#x5165;&#x53E3;/&#x540E;&#x53F0;&#x6570;&#x636E;&#x5904;&#x7406; app.js</h2><p>&#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install body-parser moment ejs --save</div></pre></td></tr></table></figure>
<blockquote>
<p>app.js</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&apos;express&apos;</span>);</div><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">const</span> db = <span class="built_in">require</span>(<span class="string">&apos;./db&apos;</span>);</div><div class="line"><span class="keyword">const</span> bodyParser = <span class="built_in">require</span>(<span class="string">&apos;body-parser&apos;</span>);</div><div class="line"><span class="keyword">const</span> moment = <span class="built_in">require</span>(<span class="string">&apos;moment&apos;</span>);</div><div class="line"></div><div class="line"><span class="keyword">const</span> app = express();</div><div class="line"></div><div class="line"><span class="comment">// &#x516C;&#x5F00;&#x9759;&#x6001;&#x8D44;&#x6E90;</span></div><div class="line">app.use(<span class="string">&apos;/node_modules&apos;</span>, express.static(path.join(__dirname, <span class="string">&apos;node_modules&apos;</span>)));</div><div class="line">app.use(<span class="string">&apos;/public&apos;</span>, express.static(path.join(__dirname, <span class="string">&apos;public&apos;</span>)));</div><div class="line"> </div><div class="line"><span class="comment">// &#x914D;&#x7F6E;&#x6A21;&#x677F;&#x5F15;&#x64CE;</span></div><div class="line"><span class="comment">// &#x914D;&#x7F6E;&#x89C6;&#x56FE;&#x5B58;&#x653E;&#x8DEF;&#x5F84;&#xFF0C;&#x4E0D;&#x914D;&#x7F6E;&#x9ED8;&#x8BA4;&#x5C31;&#x662F; views</span></div><div class="line">app.set(<span class="string">&apos;views&apos;</span>, path.join(__dirname, <span class="string">&apos;views&apos;</span>));</div><div class="line"><span class="comment">// &#x9ED8;&#x8BA4;&#x540E;&#x7F00;&#x540D;&#x662F; ejs&#xFF0C;&#x8FD9;&#x6837;&#x914D;&#x7F6E;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; html &#x540E;&#x7F00;&#x540D;</span></div><div class="line"><span class="comment">// xTemplate &#x6A21;&#x677F;&#x5F15;&#x64CE;&#x914D;&#x7F6E;&#x4E5F;&#x662F;&#x5982;&#x6B64;</span></div><div class="line">app.engine(<span class="string">&apos;.html&apos;</span>, <span class="built_in">require</span>(<span class="string">&apos;ejs&apos;</span>).__express);</div><div class="line">app.set(<span class="string">&apos;view engine&apos;</span>, <span class="string">&apos;html&apos;</span>);</div><div class="line"></div><div class="line"><span class="comment">// &#x914D;&#x7F6E;&#x89E3;&#x6790;&#x666E;&#x901A;&#x8868;&#x5355; POST &#x8BF7;&#x6C42;&#x4F53;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;</span></div><div class="line"><span class="comment">// &#x4EFB;&#x4F55;&#x8BF7;&#x6C42;&#x8FDB;&#x6765;&#xFF0C;&#x5982;&#x679C;&#x662F; POST &#x8BF7;&#x6C42;&#xFF0C;&#x5219;&#x8BE5;&#x4E2D;&#x95F4;&#x4EF6;&#x4F1A;&#x81EA;&#x52A8;&#x89E3;&#x6790; POST &#x8BF7;&#x6C42;&#x4F53;</span></div><div class="line"><span class="comment">// &#x89E3;&#x6790;&#x6210;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x6302;&#x8F7D;&#x7ED9; req &#x8BF7;&#x6C42;&#x5BF9;&#x8C61;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#xFF1A;body&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;&#x4E0B;&#x4E00;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;</span></div><div class="line"><span class="comment">// &#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5728;&#x540E;&#x7EED;&#x67D0;&#x4E00;&#x4E2A;&#x88AB;&#x5339;&#x914D;&#x5230;&#x7684;&#x5904;&#x7406;&#x51FD;&#x6570;&#x4E2D;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x901A;&#x8FC7; req.body &#x6765;&#x4F7F;&#x7528;&#x8868;&#x5355; POST &#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E;</span></div><div class="line">app.use(bodyParser.urlencoded({ <span class="attr">extended</span>: <span class="literal">false</span> }));</div><div class="line">app.use(bodyParser.json());</div><div class="line"></div><div class="line"><span class="comment">// &#x8BF7;&#x6C42;&#x7F51;&#x7AD9;&#x6839;&#x8DEF;&#x5F84;</span></div><div class="line">app.get(<span class="string">&apos;/&apos;</span>, (req, res) =&gt; {</div><div class="line">  <span class="comment">// &#x6E32;&#x67D3;&#x9996;&#x9875;</span></div><div class="line">  res.render(<span class="string">&apos;index&apos;</span>);</div><div class="line">});</div><div class="line"></div><div class="line">app.get(<span class="string">&apos;/message&apos;</span>, (req, res, next) =&gt; {</div><div class="line">  <span class="comment">// 1. &#x67E5;&#x8BE2;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x5185;&#x5BB9;</span></div><div class="line">  <span class="comment">// 2. &#x53EF;&#x80FD;&#x4F1A;&#x5904;&#x7406;&#x4E00;&#x4E0B;&#x83B7;&#x53D6;&#x5230;&#x7684;&#x6570;&#x636E;</span></div><div class="line">  <span class="comment">// 3. &#x53D1;&#x9001;&#x54CD;&#x5E94;</span></div><div class="line">  db.query(<span class="string">&apos;SELECT * FROM `feedback` ORDER BY `date` DESC&apos;</span>, (err, rows) =&gt; { <span class="comment">// &#x6309;&#x65E5;&#x671F;&#x6392;&#x5E8F;</span></div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> next(err);</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x5904;&#x7406;&#x65F6;&#x95F4;</span></div><div class="line">    rows.forEach(<span class="function"><span class="params">r</span> =&gt;</span> r.date = moment(r.date).format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>));</div><div class="line">    res.json({</div><div class="line">      <span class="attr">messages</span>: rows</div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line">app.get(<span class="string">&apos;/signup&apos;</span>, (req, res) =&gt; {</div><div class="line">  res.render(<span class="string">&apos;signup&apos;</span>);</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// &#x6CE8;&#x518C;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#xFF08;&#x5F02;&#x6B65;&#x8BF7;&#x6C42;&#x8868;&#x5355;&#x4E0D;&#x4F1A;&#x5237;&#x65B0;&#xFF09;</span></div><div class="line">app.post(<span class="string">&apos;/signup&apos;</span>, (req, res, next) =&gt; {</div><div class="line">  <span class="comment">// 1. &#x63A5;&#x6536;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#xFF1A;&#x505A;&#x6570;&#x636E;&#x7684;&#x5408;&#x6CD5;&#x6027;&#x6821;&#x9A8C;</span></div><div class="line">  <span class="comment">// 2. &#x5904;&#x7406;&#x8BF7;&#x6C42;&#xFF1A;&#x5148;&#x6821;&#x9A8C;&#x7528;&#x6237;&#x540D;&#x662F;&#x5426;&#x88AB;&#x5360;&#x7528;&#x3001;</span></div><div class="line">  <span class="comment">//    &#x5982;&#x679C;&#x5DF2;&#x5B58;&#x5728;&#xFF0C;&#x544A;&#x8BC9;&#x7528;&#x6237;&#xFF0C;&#x7528;&#x6237;&#x540D;&#x88AB;&#x5360;&#x7528;&#x4E86;&#xFF1B;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x6267;&#x884C;&#x6CE8;&#x518C;</span></div><div class="line">  <span class="comment">//    &#x6CE8;&#x518C;&#x6210;&#x529F;&#xFF1A;&#x544A;&#x8BC9;&#x7528;&#x6237;&#xFF1A;&#x64CD;&#x4F5C;&#x6210;&#x529F;&#xFF1B;&#x5982;&#x679C;&#x5931;&#x8D25;&#xFF0C;&#x4E5F;&#x544A;&#x8BC9;&#x7528;&#x6237;&#xFF0C;&#x64CD;&#x4F5C;&#x5931;&#x8D25;</span></div><div class="line">  <span class="comment">// 3. &#x53D1;&#x9001;&#x54CD;&#x5E94;</span></div><div class="line">  <span class="keyword">const</span> body = req.body;</div><div class="line"></div><div class="line">  <span class="comment">// &#x524D;&#x7AEF;&#x4F20;&#x9012;&#x7ED9;&#x540E;&#x53F0;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x518D;&#x6B21;&#x505A;&#x5408;&#x6CD5;&#x6027;&#x6821;&#x9A8C;</span></div><div class="line"></div><div class="line">  db.query(<span class="string">&apos;SELECT * FROM `users` WHERE `username`=?&apos;</span>, [body.username], (err, rows) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> next(err);</div><div class="line">    }</div><div class="line">    <span class="keyword">if</span> (rows[<span class="number">0</span>]) {</div><div class="line">      <span class="comment">// &#x8BF4;&#x660E;&#x88AB;&#x5360;&#x7528;&#x4E86;</span></div><div class="line">      <span class="keyword">return</span> res.json({</div><div class="line">        <span class="comment">// &#x5F02;&#x6B65;&#x4EA4;&#x4E92;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x4E1A;&#x52A1;&#x72B6;&#x6001;&#x7801;</span></div><div class="line">        code: <span class="number">2001</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;username already exists&apos;</span></div><div class="line">      });</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x6267;&#x884C;&#x5230;&#x8FD9;&#x91CC;&#xFF0C;&#x53EF;&#x4EE5;&#x63D2;&#x5165;&#x6570;&#x636E;&#x4E86;</span></div><div class="line">    db.query(<span class="string">&apos;INSERT INTO `users`(`username`, `password`, `email`, `create_time`, `last_signin_time`) VALUES(?, ?, ?, ?, ?)&apos;</span>, [</div><div class="line">      body.username,</div><div class="line">      body.password,</div><div class="line">      body.email,</div><div class="line">      moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>),</div><div class="line">      moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>)</div><div class="line">    ], (err, stat) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> next(err);</div><div class="line">      }</div><div class="line">      res.json({</div><div class="line">        <span class="attr">code</span>: <span class="number">2000</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;success&apos;</span>,</div><div class="line">      });</div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line">app.get(<span class="string">&apos;/signin&apos;</span>, (req, res) =&gt; {</div><div class="line">  res.render(<span class="string">&apos;signin&apos;</span>);</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// post &#x5904;&#x7406;&#x7559;&#x8A00;&#x9875;&#x9762;&#x5F02;&#x6B65;&#x8868;&#x5355;&#x63D0;&#x4EA4;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0D;&#x4F7F;&#x7528; form&#xFF0C;&#x4F46;&#x662F;&#x6700;&#x597D;&#x52A0;&#x4E0A;&#xFF1B;&#x540E;&#x53F0;&#x4F1A;&#x6307;&#x5B9A;&#x9700;&#x8981;&#x54EA;&#x4E9B;&#x6570;&#x636E;</span></div><div class="line"><span class="comment">// body { message: &apos;&apos;, name: &apos;&apos;, email: &apos;&apos; }</span></div><div class="line">app.post(<span class="string">&apos;/message/add&apos;</span>, (req, res, next) =&gt; {</div><div class="line">  <span class="comment">// 1. &#x63A5;&#x6536;&#x5BA2;&#x6237;&#x7AEF;&#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E;</span></div><div class="line">  <span class="comment">// 2. &#x5904;&#x7406;&#x5BA2;&#x6237;&#x7AEF;&#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E;&#xFF1A;2.1&#x3001;&#x5BF9;&#x6570;&#x636E;&#x505A;&#x5408;&#x6CD5;&#x6027;&#x6821;&#x9A8C;&#xFF1B;2.2&#x3001; &#x6821;&#x9A8C;&#x901A;&#x8FC7;&#x4E4B;&#x540E;&#xFF0C;&#x63D2;&#x5165;&#x6570;&#x636E;&#x5E93;</span></div><div class="line">  <span class="comment">// 3. &#x7ED9;&#x5F53;&#x524D;&#x8BF7;&#x6C42;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x54CD;&#x5E94;</span></div><div class="line">  <span class="keyword">const</span> body = req.body;</div><div class="line">  db.query(<span class="string">&apos;INSERT INTO `feedback`(`message`, `name`, `email`, `date`) VALUES(?, ?, ?, ?)&apos;</span>, [</div><div class="line">    body.message,</div><div class="line">    body.name,</div><div class="line">    body.email,</div><div class="line">    moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>)</div><div class="line">  ], (err, stat) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> next(err);</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x91CD;&#x5B9A;&#x5411;&#x5BF9; ajax &#x8BF7;&#x6C42;&#x6CA1;&#x6709;&#x7528;&#xFF0C;&#x91CD;&#x5B9A;&#x5411;&#x53EA;&#x5BF9;&#x5BA2;&#x6237;&#x7AEF;&#x540C;&#x6B65;&#x8BF7;&#x6C42;&#x6709;&#x6548;</span></div><div class="line">    <span class="comment">// &#x4F8B;&#x5982;&#x8F93;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x5730;&#x5740;&#x6572;&#x56DE;&#x8F66;&#xFF0C;&#x70B9;&#x4E86;&#x4E00;&#x4E2A; a &#x8FDE;&#x63A5;&#xFF0C;&#x63D0;&#x4EA4;&#x4E86;&#x4E00;&#x4E2A;&#x8868;&#x5355;&#x90FD;&#x662F;&#x540C;&#x6B65;&#x8BF7;&#x6C42;</span></div><div class="line">    res.json({</div><div class="line">      <span class="attr">code</span>: <span class="number">1000</span>,</div><div class="line">      <span class="attr">message</span>: <span class="string">&apos;success&apos;</span></div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// &#x5343;&#x4E07;&#x4E0D;&#x8981;&#x5C11;&#x4E9B;&#x6700;&#x540E;&#x4E00;&#x4E2A; next</span></div><div class="line"><span class="comment">// &#x5982;&#x679C;&#x5C11;&#x4E86;&#x6700;&#x540E;&#x7684;&#x53C2;&#x6570; next&#xFF0C;&#x5219;&#x73B0;&#x5728;&#x8FD9;&#x4E2A;&#x4E2D;&#x95F4;&#x4EF6;&#x5C31;&#x662F;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x7684;&#x4E2D;&#x95F4;&#x4EF6; req res next</span></div><div class="line"><span class="comment">// &#x6240;&#x4EE5;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;&#x4E00;&#x5B9A;&#x8981;&#x4F7F;&#x7528;&#x56DB;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;err req res next</span></div><div class="line"><span class="comment">// &#x53EA;&#x8981;&#x5728;&#x4E4B;&#x524D;&#x7684;&#x4EFB;&#x4F55;&#x4E2D;&#x95F4;&#x4EF6;&#x4E2D;&#x8C03;&#x7528; next &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F20;&#x9012;&#x4E86;&#x53C2;&#x6570;&#xFF0C;&#x5C31;&#x4E00;&#x5B9A;&#x4F1A;&#x6267;&#x884C;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;</span></div><div class="line">app.use(<span class="function">(<span class="params">err, req, res, next</span>) =&gt;</span> {</div><div class="line">  res.send(<span class="string">`500 ErrorMessage:<span class="subst">${err.message}</span>`</span>);</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// &#x76D1;&#x542C;&#x7AEF;&#x53E3;&#xFF0C;&#x542F;&#x52A8;&#x670D;&#x52A1;</span></div><div class="line">app.listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;server is running at port 3000.&apos;</span>);</div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x6E32;&#x67D3;&#x9875;&#x9762;-EJS"><a href="#&#x6E32;&#x67D3;&#x9875;&#x9762;-EJS" class="headerlink" title="&#x6E32;&#x67D3;&#x9875;&#x9762; EJS"></a>&#x6E32;&#x67D3;&#x9875;&#x9762; EJS</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">{{each messages as message index}}</div><div class="line"><span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span>{{message.name}}&#xFF1A;{{message.message}}<span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;pull-right&quot;</span>&gt;</span>{{message.date}}<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></div><div class="line">{{/each}}</div></pre></td></tr></table></figure>
<h2 id="&#x524D;&#x53F0;-ajax-&#x8BF7;&#x6C42;"><a href="#&#x524D;&#x53F0;-ajax-&#x8BF7;&#x6C42;" class="headerlink" title="&#x524D;&#x53F0; ajax &#x8BF7;&#x6C42;"></a>&#x524D;&#x53F0; ajax &#x8BF7;&#x6C42;</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">$(<span class="string">&apos;#form&apos;</span>).on(<span class="string">&apos;submit&apos;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>{</div><div class="line">  <span class="comment">// &#x52A0;&#x4E0A;&#x8FD9;&#x53E5;&#xFF0C;&#x8868;&#x5355;&#x4E0D;&#x4F1A;&#x9ED8;&#x8BA4;&#x63D0;&#x4EA4;&#x4E86;</span></div><div class="line">  e.preventDefault();</div><div class="line">  $.ajax({</div><div class="line">      <span class="attr">url</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;action&apos;</span>),</div><div class="line">      <span class="attr">type</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;method&apos;</span>),</div><div class="line">      <span class="attr">data</span>: $(<span class="keyword">this</span>).serialize(),</div><div class="line">      <span class="attr">dataType</span>: <span class="string">&apos;json&apos;</span></div><div class="line">    })</div><div class="line">    .then(<span class="function"><span class="params">data</span> =&gt;</span> {</div><div class="line">      <span class="comment">// &#x662F;&#x5426;&#x6210;&#x529F;&#x7531;&#x540E;&#x53F0;&#x544A;&#x8BC9;&#x524D;&#x53F0;</span></div><div class="line">      <span class="keyword">if</span> (data.code === <span class="number">1000</span>) {</div><div class="line">        $(<span class="string">&apos;#form [name]&apos;</span>).val(<span class="string">&apos;&apos;</span>);</div><div class="line">        loadMessages();</div><div class="line">        <span class="built_in">window</span>.alert(<span class="string">&apos;&#x7559;&#x8A00;&#x6210;&#x529F;&apos;</span>);</div><div class="line">      }</div><div class="line">    });</div><div class="line">   <span class="comment">// &#x548C; e.preventDefault(); &#x4F5C;&#x7528;&#x76F8;&#x540C;</span></div><div class="line">  <span class="comment">// return false;</span></div><div class="line">});</div></pre></td></tr></table></figure>
<h2 id="&#x540E;&#x53F0;-post-&#x8BF7;&#x6C42;&#x5904;&#x7406;"><a href="#&#x540E;&#x53F0;-post-&#x8BF7;&#x6C42;&#x5904;&#x7406;" class="headerlink" title="&#x540E;&#x53F0; post &#x8BF7;&#x6C42;&#x5904;&#x7406;"></a>&#x540E;&#x53F0; post &#x8BF7;&#x6C42;&#x5904;&#x7406;</h2><blockquote>
<p>express &#x7684; body-parser &#x4E2D;&#x95F4;&#x952E;</p>
</blockquote>
<p>&#x914D;&#x7F6E;&#x4E2D;&#x95F4;&#x952E;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">app.use(bodyParser.urlencoded({ <span class="attr">extended</span>: <span class="literal">false</span> }));</div><div class="line">app.use(bodyParser.json());</div></pre></td></tr></table></figure>
<p>&#x65E5;&#x671F;&#x683C;&#x5F0F;&#x5316;&#x5305;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install moment --save</div></pre></td></tr></table></figure>
<p>&#x683C;&#x5F0F;&#x5316;&#x65E5;&#x671F;&#x65B9;&#x6CD5;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>)</div></pre></td></tr></table></figure>
<p>&#x540E;&#x53F0;&#x5904;&#x7406; post &#x8BF7;&#x6C42;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">app.post(<span class="string">&apos;/message/add&apos;</span>, (req, res, next) =&gt; {</div><div class="line">    <span class="keyword">const</span> body = req.body;</div><div class="line">    db.query(<span class="string">&apos;INSERT INTO `feedback`(`message`, `name`, `email`, `date`) VALUES(?, ?, ?, ?)&apos;</span>, [</div><div class="line">      body.message,</div><div class="line">      body.name,</div><div class="line">      body.email,</div><div class="line">      moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>)</div><div class="line">    ], (err, stat) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> next(err);</div><div class="line">      }</div><div class="line">      res.json({</div><div class="line">        <span class="attr">code</span>: <span class="number">1000</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;success&apos;</span></div><div class="line">      });</div><div class="line">    });</div><div class="line">});</div></pre></td></tr></table></figure>
<p>&#x54CD;&#x5E94;&#x7ED9;&#x524D;&#x53F0;&#x540E;&#xFF0C;&#x524D;&#x53F0;&#x5904;&#x7406;&#x54CD;&#x5E94;&#x6210;&#x529F;&#x903B;&#x8F91;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x662F;&#x5426;&#x6210;&#x529F;&#x7531;&#x540E;&#x53F0;&#x544A;&#x8BC9;&#x524D;&#x53F0;</span></div><div class="line"><span class="keyword">if</span> (data.code === <span class="number">1000</span>) {</div><div class="line">  $(<span class="string">&apos;#form [name]&apos;</span>).val(<span class="string">&apos;&apos;</span>);</div><div class="line">  loadMessages();</div><div class="line">  <span class="built_in">window</span>.alert(<span class="string">&apos;&#x7559;&#x8A00;&#x6210;&#x529F;&apos;</span>);</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="&#x6CE8;&#x518C;&#x767B;&#x5F55;&#x6A21;&#x5757;"><a href="#&#x6CE8;&#x518C;&#x767B;&#x5F55;&#x6A21;&#x5757;" class="headerlink" title="&#x6CE8;&#x518C;&#x767B;&#x5F55;&#x6A21;&#x5757;"></a>&#x6CE8;&#x518C;&#x767B;&#x5F55;&#x6A21;&#x5757;</h2><h3 id="&#x767B;&#x5F55;&#x9875;&#x9762;"><a href="#&#x767B;&#x5F55;&#x9875;&#x9762;" class="headerlink" title="&#x767B;&#x5F55;&#x9875;&#x9762;"></a>&#x767B;&#x5F55;&#x9875;&#x9762;</h3><h3 id="&#x6CE8;&#x518C;&#x9875;&#x9762;"><a href="#&#x6CE8;&#x518C;&#x9875;&#x9762;" class="headerlink" title="&#x6CE8;&#x518C;&#x9875;&#x9762;"></a>&#x6CE8;&#x518C;&#x9875;&#x9762;</h3><h3 id="&#x6D41;&#x7A0B;"><a href="#&#x6D41;&#x7A0B;" class="headerlink" title="&#x6D41;&#x7A0B;"></a>&#x6D41;&#x7A0B;</h3><h4 id="&#x6DFB;&#x52A0;&#x8868;-users"><a href="#&#x6DFB;&#x52A0;&#x8868;-users" class="headerlink" title="&#x6DFB;&#x52A0;&#x8868; users"></a>&#x6DFB;&#x52A0;&#x8868; users</h4><table>
<thead>
<tr>
<th>&#x7D22;&#x5F15;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x957F;&#x5EA6;</th>
<th>&#x5C0F;&#x6570;&#x70B9;</th>
<th>&#x4E0D;&#x662F;null</th>
<th>&#x4E3B;&#x952E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>int</td>
<td>50</td>
<td></td>
<td>yes</td>
<td>yes</td>
</tr>
<tr>
<td>username</td>
<td>varchar</td>
<td>50</td>
<td></td>
<td>yes</td>
<td></td>
</tr>
<tr>
<td>password</td>
<td>varchar</td>
<td>50</td>
<td></td>
<td>yes</td>
<td></td>
</tr>
<tr>
<td>email</td>
<td>varchar</td>
<td>50</td>
<td></td>
<td>yes</td>
<td></td>
</tr>
<tr>
<td>create_time</td>
<td>datetime</td>
<td></td>
<td></td>
<td>yes</td>
<td></td>
</tr>
<tr>
<td>last_signin_time</td>
<td>datetime</td>
<td></td>
<td></td>
<td>yes</td>
</tr>
</tbody>
</table>
<h4 id="&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x540E;&#x53F0;&#x903B;&#x8F91;"><a href="#&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x540E;&#x53F0;&#x903B;&#x8F91;" class="headerlink" title="&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x540E;&#x53F0;&#x903B;&#x8F91;"></a>&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x540E;&#x53F0;&#x903B;&#x8F91;</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x6CE8;&#x518C;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#xFF08;&#x5F02;&#x6B65;&#x8BF7;&#x6C42;&#x8868;&#x5355;&#x4E0D;&#x4F1A;&#x5237;&#x65B0;&#xFF09;</span></div><div class="line">app.post(<span class="string">&apos;/signup&apos;</span>, (req, res, next) =&gt; {</div><div class="line">  <span class="comment">// 1. &#x63A5;&#x6536;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#xFF1A;&#x505A;&#x6570;&#x636E;&#x7684;&#x5408;&#x6CD5;&#x6027;&#x6821;&#x9A8C;</span></div><div class="line">  <span class="comment">// 2. &#x5904;&#x7406;&#x8BF7;&#x6C42;&#xFF1A;&#x5148;&#x6821;&#x9A8C;&#x7528;&#x6237;&#x540D;&#x662F;&#x5426;&#x88AB;&#x5360;&#x7528;&#x3001;</span></div><div class="line">  <span class="comment">//    &#x5982;&#x679C;&#x5DF2;&#x5B58;&#x5728;&#xFF0C;&#x544A;&#x8BC9;&#x7528;&#x6237;&#xFF0C;&#x7528;&#x6237;&#x540D;&#x88AB;&#x5360;&#x7528;&#x4E86;&#xFF1B;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x6267;&#x884C;&#x6CE8;&#x518C;</span></div><div class="line">  <span class="comment">//    &#x6CE8;&#x518C;&#x6210;&#x529F;&#xFF1A;&#x544A;&#x8BC9;&#x7528;&#x6237;&#xFF1A;&#x64CD;&#x4F5C;&#x6210;&#x529F;&#xFF1B;&#x5982;&#x679C;&#x5931;&#x8D25;&#xFF0C;&#x4E5F;&#x544A;&#x8BC9;&#x7528;&#x6237;&#xFF0C;&#x64CD;&#x4F5C;&#x5931;&#x8D25;</span></div><div class="line">  <span class="comment">// 3. &#x53D1;&#x9001;&#x54CD;&#x5E94;</span></div><div class="line">  <span class="keyword">const</span> body = req.body;</div><div class="line"></div><div class="line">  <span class="comment">// &#x524D;&#x7AEF;&#x4F20;&#x9012;&#x7ED9;&#x540E;&#x53F0;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x518D;&#x6B21;&#x505A;&#x5408;&#x6CD5;&#x6027;&#x6821;&#x9A8C;</span></div><div class="line">  db.query(<span class="string">&apos;SELECT * FROM `users` WHERE `username`=?&apos;</span>, [body.username], (err, rows) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> next(err);</div><div class="line">    }</div><div class="line">    <span class="keyword">if</span> (rows[<span class="number">0</span>]) {</div><div class="line">      <span class="comment">// &#x8BF4;&#x660E;&#x88AB;&#x5360;&#x7528;&#x4E86;</span></div><div class="line">      <span class="keyword">return</span> res.json({</div><div class="line">        <span class="comment">// &#x5F02;&#x6B65;&#x4EA4;&#x4E92;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x4E1A;&#x52A1;&#x72B6;&#x6001;&#x7801;</span></div><div class="line">        code: <span class="number">2001</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;username already exists&apos;</span></div><div class="line">      });</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x6267;&#x884C;&#x5230;&#x8FD9;&#x91CC;&#xFF0C;&#x53EF;&#x4EE5;&#x63D2;&#x5165;&#x6570;&#x636E;&#x4E86;</span></div><div class="line">    db.query(<span class="string">&apos;INSERT INTO `users`(`username`, `password`, `email`, `create_time`, `last_signin_time`) VALUES(?, ?, ?, ?, ?)&apos;</span>, [</div><div class="line">      body.username,</div><div class="line">      body.password,</div><div class="line">      body.email,</div><div class="line">      moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>),</div><div class="line">      moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>)</div><div class="line">    ], (err, stat) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> next(err);</div><div class="line">      }</div><div class="line">      res.json({</div><div class="line">        <span class="attr">code</span>: <span class="number">2000</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;success&apos;</span>,</div><div class="line">      });</div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div></pre></td></tr></table></figure>
<h4 id="&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x524D;&#x53F0;&#x903B;&#x8F91;"><a href="#&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x524D;&#x53F0;&#x903B;&#x8F91;" class="headerlink" title="&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x524D;&#x53F0;&#x903B;&#x8F91;"></a>&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;&#x524D;&#x53F0;&#x903B;&#x8F91;</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">$(<span class="string">&apos;form&apos;</span>).on(<span class="string">&apos;submit&apos;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>{</div><div class="line">  e.preventDefault();</div><div class="line">  $.ajax({</div><div class="line">      <span class="attr">url</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;action&apos;</span>),</div><div class="line">      <span class="attr">type</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;method&apos;</span>),</div><div class="line">      <span class="attr">data</span>: $(<span class="keyword">this</span>).serialize(),</div><div class="line">      <span class="attr">dataType</span>: <span class="string">&apos;json&apos;</span></div><div class="line">    })</div><div class="line">    .then(<span class="function"><span class="params">data</span> =&gt;</span> {</div><div class="line">      <span class="keyword">if</span> (data.code === <span class="number">2000</span>) {</div><div class="line">        <span class="built_in">window</span>.alert(<span class="string">&apos;&#x606D;&#x559C;&#xFF1A;&#x6CE8;&#x518C;&#x6210;&#x529F;&apos;</span>);</div><div class="line">        <span class="comment">// &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x63A7;&#x5236; location &#x7684; href &#xFF0C;&#x8BA9;&#x6D4F;&#x89C8;&#x5668;&#x8DF3;&#x8F6C;</span></div><div class="line">        <span class="built_in">window</span>.location.href = <span class="string">&apos;/&apos;</span>;</div><div class="line">      } <span class="keyword">else</span> <span class="keyword">if</span> (data.code === <span class="number">2001</span>) {</div><div class="line">        <span class="built_in">window</span>.alert(<span class="string">&apos;&#x7528;&#x6237;&#x540D;&#x5DF2;&#x5B58;&#x5728;&#xFF0C;&#x8BF7;&#x66F4;&#x6362;&#x91CD;&#x8BD5;&apos;</span>);</div><div class="line">      }</div><div class="line">    });</div><div class="line">});</div></pre></td></tr></table></figure>
<h4 id="&#x8BB0;&#x4F4F;&#x7528;&#x6237;&#x540D;&#xFF08;&#x901A;&#x8FC7;&#x524D;&#x53F0;&#x6765;&#x5199;&#xFF09;"><a href="#&#x8BB0;&#x4F4F;&#x7528;&#x6237;&#x540D;&#xFF08;&#x901A;&#x8FC7;&#x524D;&#x53F0;&#x6765;&#x5199;&#xFF09;" class="headerlink" title="&#x8BB0;&#x4F4F;&#x7528;&#x6237;&#x540D;&#xFF08;&#x901A;&#x8FC7;&#x524D;&#x53F0;&#x6765;&#x5199;&#xFF09;"></a>&#x8BB0;&#x4F4F;&#x7528;&#x6237;&#x540D;&#xFF08;&#x901A;&#x8FC7;&#x524D;&#x53F0;&#x6765;&#x5199;&#xFF09;</h4><blockquote>
<p> &#x65B9;&#x6CD5;&#x4E00;&#xFF1A;</p>
</blockquote>
<p>&#x5B89;&#x88C5;&#x63D2;&#x4EF6;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install js-cookie --save</div></pre></td></tr></table></figure>
<p>&#x8BBE;&#x7F6E; cookie&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> username = Cookies.get(<span class="string">&apos;username&apos;</span>);</div><div class="line"><span class="keyword">if</span> (username) {</div><div class="line">  <span class="built_in">document</span>.querySelector(<span class="string">&apos;#username&apos;</span>).value = username;</div><div class="line">}</div><div class="line">$(<span class="string">&apos;form&apos;</span>).on(<span class="string">&apos;submit&apos;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>{</div><div class="line">  e.preventDefault();</div><div class="line">  Cookies.set(<span class="string">&apos;username&apos;</span>, $(<span class="string">&apos;#username&apos;</span>).val());</div><div class="line">});</div></pre></td></tr></table></figure>
<h5 id="&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;-cookie-parser"><a href="#&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;-cookie-parser" class="headerlink" title="&#x5728; express &#x4E2D;&#x4F7F;&#x7528; cookie-parser"></a>&#x5728; express &#x4E2D;&#x4F7F;&#x7528; cookie-parser</h5><p>&#x5B89;&#x88C5;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install cookie-parser --save</div></pre></td></tr></table></figure>
<p>&#x914D;&#x7F6E;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&apos;express&apos;</span>);</div><div class="line"><span class="keyword">const</span> cookieParser = <span class="built_in">require</span>(<span class="string">&apos;cookie-parser&apos;</span>);</div><div class="line"><span class="keyword">const</span> app = express();</div><div class="line">app.use(cookieParser());</div></pre></td></tr></table></figure>
<blockquote>
<p>&#x65B9;&#x6CD5;&#x4E8C;&#xFF1A;session &#x4FDD;&#x6301;&#x72B6;&#x6001;</p>
</blockquote>
<p>session &#x548C; cookie &#x7ED3;&#x5408;&#x4F7F;&#x7528;&#x3002;</p>
<h5 id="&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;-session"><a href="#&#x5728;-express-&#x4E2D;&#x4F7F;&#x7528;-session" class="headerlink" title="&#x5728; express &#x4E2D;&#x4F7F;&#x7528; session"></a>&#x5728; express &#x4E2D;&#x4F7F;&#x7528; session</h5><p>&#x5B89;&#x88C5;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install express-session --save</div></pre></td></tr></table></figure>
<p>&#x914D;&#x7F6E;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&apos;express&apos;</span>);</div><div class="line"><span class="keyword">const</span> session = <span class="built_in">require</span>(<span class="string">&apos;express-session&apos;</span>);</div><div class="line"><span class="keyword">const</span> app = express();</div><div class="line">app.use(session({</div><div class="line">  <span class="comment">// &#x8868;&#x793A;&#x751F;&#x6210;&#x94A5;&#x5319;&#x7684;&#x65F6;&#x5019;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x751F;&#x6210;</span></div><div class="line">  secret: <span class="string">&apos;mhq&apos;</span>,</div><div class="line">  <span class="attr">resave</span>: <span class="literal">false</span>,</div><div class="line">  <span class="attr">saveUnitialized</span>: <span class="literal">true</span></div><div class="line">}));</div></pre></td></tr></table></figure>
<h3 id="&#x5904;&#x7406;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x548C;&#x767B;&#x5F55;&#x7684;&#x72B6;&#x6001;"><a href="#&#x5904;&#x7406;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x548C;&#x767B;&#x5F55;&#x7684;&#x72B6;&#x6001;" class="headerlink" title="&#x5904;&#x7406;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x548C;&#x767B;&#x5F55;&#x7684;&#x72B6;&#x6001;"></a>&#x5904;&#x7406;&#x7528;&#x6237;&#x6CE8;&#x518C;&#x548C;&#x767B;&#x5F55;&#x7684;&#x72B6;&#x6001;</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install express-session --save</div></pre></td></tr></table></figure>
<h4 id="&#x6700;&#x7EC8;&#x7684;-app-js"><a href="#&#x6700;&#x7EC8;&#x7684;-app-js" class="headerlink" title="&#x6700;&#x7EC8;&#x7684; app.js"></a>&#x6700;&#x7EC8;&#x7684; app.js</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x5F15;&#x5165;&#x5305;</span></div><div class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&apos;express&apos;</span>);</div><div class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&apos;path&apos;</span>);</div><div class="line"><span class="keyword">const</span> db = <span class="built_in">require</span>(<span class="string">&apos;./db&apos;</span>);</div><div class="line"><span class="keyword">const</span> bodyParser = <span class="built_in">require</span>(<span class="string">&apos;body-parser&apos;</span>);</div><div class="line"><span class="keyword">const</span> moment = <span class="built_in">require</span>(<span class="string">&apos;moment&apos;</span>);</div><div class="line"><span class="keyword">const</span> session = <span class="built_in">require</span>(<span class="string">&apos;express-session&apos;</span>);</div><div class="line"></div><div class="line"><span class="keyword">const</span> app = express();</div><div class="line"></div><div class="line"><span class="comment">// &#x66B4;&#x9732;&#x9759;&#x6001;&#x8D44;&#x6E90;</span></div><div class="line">app.use(<span class="string">&apos;/node_modules&apos;</span>, express.static(path.join(__dirname, <span class="string">&apos;node_modules&apos;</span>)));</div><div class="line">app.use(<span class="string">&apos;/public&apos;</span>, express.static(path.join(__dirname, <span class="string">&apos;public&apos;</span>)));</div><div class="line"></div><div class="line"><span class="comment">// &#x914D;&#x7F6E;&#x6A21;&#x677F;&#x5F15;&#x64CE;</span></div><div class="line">app.set(<span class="string">&apos;views&apos;</span>, path.join(__dirname, <span class="string">&apos;views&apos;</span>));</div><div class="line">app.engine(<span class="string">&apos;.html&apos;</span>, <span class="built_in">require</span>(<span class="string">&apos;ejs&apos;</span>).__express);</div><div class="line">app.set(<span class="string">&apos;view engine&apos;</span>, <span class="string">&apos;html&apos;</span>);</div><div class="line"></div><div class="line"><span class="comment">// &#x914D;&#x7F6E; body-parser &#x4E2D;&#x95F4;&#x4EF6;</span></div><div class="line">app.use(bodyParser.urlencoded({ <span class="attr">extended</span>: <span class="literal">false</span> }));</div><div class="line">app.use(bodyParser.json());</div><div class="line"></div><div class="line"><span class="comment">// &#x914D;&#x7F6E; Session &#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x5728;&#x8FDB;&#x5165;&#x8DEF;&#x7531;&#x4E4B;&#x524D;&#x914D;&#x7F6E;</span></div><div class="line">app.use(session({</div><div class="line">  <span class="attr">secret</span>: <span class="string">&apos;mhq&apos;</span>,</div><div class="line">  <span class="attr">resave</span>: <span class="literal">false</span>,</div><div class="line">  <span class="attr">saveUninitialized</span>: <span class="literal">true</span></div><div class="line">}));</div><div class="line"></div><div class="line"><span class="comment">// &#x6CA1;&#x6709;&#x767B;&#x5F55;&#x65F6;&#x8DF3;&#x5230;&#x767B;&#x5F55;&#x9875;&#x9762;</span></div><div class="line">app.get(<span class="string">&apos;/&apos;</span>, (req, res) =&gt; {</div><div class="line">  <span class="keyword">if</span> (!req.session.user) {</div><div class="line">    <span class="keyword">return</span> res.redirect(<span class="string">&apos;/signin&apos;</span>);</div><div class="line">  }</div><div class="line">  res.render(<span class="string">&apos;index&apos;</span>, {</div><div class="line">    <span class="comment">// session &#x4E2D;&#x4FDD;&#x5B58;&#x4E86; user</span></div><div class="line">    user: req.session.user</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x6D88;&#x606F;&#x9875;&#x9762;&#x7684; form &#x8868;&#x5355;&#x6570;&#x636E;</span></div><div class="line">app.get(<span class="string">&apos;/message&apos;</span>, (req, res, next) =&gt; {</div><div class="line">  db.query(<span class="string">&apos;SELECT * FROM `feedback` ORDER BY `date` DESC&apos;</span>, (err, rows) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> next(err);</div><div class="line">    }</div><div class="line">    rows.forEach(<span class="function"><span class="params">r</span> =&gt;</span> r.date = moment(r.date).format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>))</div><div class="line">    res.json({</div><div class="line">      <span class="attr">messages</span>: rows</div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// &#x767B;&#x5F55;&#x6210;&#x529F;&#x540E;&#x8DF3;&#x5230;&#x9996;&#x9875;</span></div><div class="line">app.get(<span class="string">&apos;/signup&apos;</span>, (req, res) =&gt; {</div><div class="line">  <span class="keyword">if</span> (req.session.user) {</div><div class="line">    <span class="keyword">return</span> res.redirect(<span class="string">&apos;/&apos;</span>);</div><div class="line">  }</div><div class="line">  res.render(<span class="string">&apos;signup&apos;</span>);</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x6CE8;&#x518C; post &#x8BF7;&#x6C42;</span></div><div class="line">app.post(<span class="string">&apos;/signup&apos;</span>, (req, res, next) =&gt; {</div><div class="line">  <span class="keyword">const</span> body = req.body;</div><div class="line">  db.query(<span class="string">&apos;SELECT * FROM `users` WHERE `username`=?&apos;</span>, [body.username], (err, rows) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> next(err);</div><div class="line">    }</div><div class="line">    <span class="keyword">if</span> (rows[<span class="number">0</span>]) {</div><div class="line">      <span class="comment">// &#x8BF4;&#x660E;&#x88AB;&#x5360;&#x7528;&#x4E86;</span></div><div class="line">      <span class="keyword">return</span> res.json({</div><div class="line">        <span class="attr">code</span>: <span class="number">2001</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;username already exists&apos;</span></div><div class="line">      });</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x6267;&#x884C;&#x5230;&#x8FD9;&#x91CC;&#xFF0C;&#x53EF;&#x4EE5;&#x63D2;&#x5165;&#x6570;&#x636E;&#x4E86;</span></div><div class="line">    db.query(<span class="string">&apos;INSERT INTO `users`(`username`, `password`, `email`, `create_time`, `last_signin_time`) VALUES(?, ?, ?, ?, ?)&apos;</span>, [</div><div class="line">      body.username,</div><div class="line">      body.password,</div><div class="line">      body.email,</div><div class="line">      moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>),</div><div class="line">      moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>)</div><div class="line">    ], (err, stat) =&gt; {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> next(err)</div><div class="line">      }</div><div class="line">      <span class="comment">// &#x6CE8;&#x518C;&#x6210;&#x529F;&#xFF0C;&#x4FDD;&#x5B58;&#x7528;&#x6237;&#x767B;&#x9646;&#x72B6;&#x6001;</span></div><div class="line">      req.session.user = {</div><div class="line">        <span class="attr">username</span>: body.username</div><div class="line">      };</div><div class="line">      res.json({</div><div class="line">        <span class="attr">code</span>: <span class="number">2000</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;success&apos;</span>,</div><div class="line">      });</div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line">app.get(<span class="string">&apos;/signin&apos;</span>, (req, res) =&gt; {</div><div class="line">  <span class="keyword">if</span> (req.session.user) {</div><div class="line">    <span class="keyword">return</span> res.redirect(<span class="string">&apos;/&apos;</span>);</div><div class="line">  }</div><div class="line">  res.render(<span class="string">&apos;signin&apos;</span>);</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x767B;&#x5F55;&#x9875;&#x9762; post &#x8BF7;&#x6C42;</span></div><div class="line">app.post(<span class="string">&apos;/signin&apos;</span>, (req, res, next) =&gt; {</div><div class="line">  <span class="keyword">const</span> username = req.body.username;</div><div class="line">  <span class="keyword">const</span> password = req.body.password;</div><div class="line">  </div><div class="line">  <span class="comment">// &#x9A8C;&#x8BC1;&#x7528;&#x6237;&#x540D;&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF0C;&#x5BC6;&#x7801;&#x662F;&#x5426;&#x6B63;&#x786E;</span></div><div class="line">  db.query(<span class="string">&apos;SELECT * FROM `users` WHERE `username`=?&apos;</span>, [username], (err, rows) =&gt; {</div><div class="line">    <span class="keyword">const</span> user = rows[<span class="number">0</span>];</div><div class="line">    <span class="keyword">if</span> (!user) {</div><div class="line">      <span class="keyword">return</span> res.json({</div><div class="line">        <span class="attr">code</span>: <span class="number">3001</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;username not exists&apos;</span></div><div class="line">      });</div><div class="line">    }</div><div class="line">    <span class="keyword">if</span> (password !== user.password) {</div><div class="line">      <span class="keyword">return</span> res.json({</div><div class="line">        <span class="attr">code</span>: <span class="number">3002</span>,</div><div class="line">        <span class="attr">message</span>: <span class="string">&apos;password error&apos;</span></div><div class="line">      })</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x767B;&#x9646;&#x6210;&#x529F;&#xFF0C;&#x4FDD;&#x5B58;&#x72B6;&#x6001;</span></div><div class="line">    req.session.user = {</div><div class="line">      <span class="attr">username</span>: username</div><div class="line">    };</div><div class="line">    res.json({</div><div class="line">      <span class="attr">code</span>: <span class="number">3000</span>,</div><div class="line">      <span class="attr">message</span>: <span class="string">&apos;login success&apos;</span></div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// &#x5904;&#x7406;&#x7559;&#x8A00;&#x9875;&#x9762;&#x7684; post &#x8BF7;&#x6C42;&#x7684;&#x6570;&#x636E;</span></div><div class="line">app.post(<span class="string">&apos;/message/add&apos;</span>, (req, res, next) =&gt; {</div><div class="line">  <span class="keyword">const</span> body = req.body;</div><div class="line">  db.query(<span class="string">&apos;INSERT INTO `feedback`(`message`, `name`, `email`, `date`) VALUES(?, ?, ?, ?)&apos;</span>, [</div><div class="line">    body.message,</div><div class="line">    body.name,</div><div class="line">    body.email,</div><div class="line">    moment().format(<span class="string">&apos;YYYY-MM-DD HH:mm:ss&apos;</span>)</div><div class="line">  ], (err, stat) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> next(err);</div><div class="line">    }</div><div class="line">    res.json({</div><div class="line">      <span class="attr">code</span>: <span class="number">1000</span>,</div><div class="line">      <span class="attr">message</span>: <span class="string">&apos;success&apos;</span></div><div class="line">    });</div><div class="line">  });</div><div class="line">});</div><div class="line"></div><div class="line">app.use(<span class="function">(<span class="params">err, req, res, next</span>) =&gt;</span> {</div><div class="line">  res.send(<span class="string">`500 ErrorMessage:<span class="subst">${err.message}</span>`</span>);</div><div class="line">});</div><div class="line">app.listen(<span class="number">3000</span>, () =&gt; {</div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;server is running at port 3000.&apos;</span>);</div><div class="line">});</div></pre></td></tr></table></figure>
<h4 id="&#x5904;&#x7406;&#x767B;&#x5F55;&#x9875;&#x9762;"><a href="#&#x5904;&#x7406;&#x767B;&#x5F55;&#x9875;&#x9762;" class="headerlink" title="&#x5904;&#x7406;&#x767B;&#x5F55;&#x9875;&#x9762;"></a>&#x5904;&#x7406;&#x767B;&#x5F55;&#x9875;&#x9762;</h4><blockquote>
<p>&#x6700;&#x7EC8;&#x7684; singin.html</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></td><td class="code"><pre><div class="line">  <span class="comment">&lt;!-- ... --&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;/signin&quot;</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-signin&quot;</span> <span class="attr">role</span>=<span class="string">&quot;form&quot;</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h2</span> <span class="attr">class</span>=<span class="string">&quot;form-signin-heading&quot;</span>&gt;</span>&#x7528;&#x6237;&#x767B;&#x9646;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">id</span>=<span class="string">&quot;username&quot;</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x7528;&#x6237;&#x540D;&quot;</span> <span class="attr">required</span> <span class="attr">autofocus</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;password&quot;</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;Password&quot;</span> <span class="attr">required</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;checkbox&quot;</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">label</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">value</span>=<span class="string">&quot;remember-me&quot;</span>&gt;</span> Remember me</div><div class="line">          <span class="tag">&lt;/<span class="name">label</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-lg btn-primary btn-block&quot;</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span>&gt;</span>&#x767B;&#x9646;<span class="tag">&lt;/<span class="name">button</span>&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">form</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;/node_modules/jquery/dist/jquery.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;/node_modules/js-cookie/src/js.cookie.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="xml"></span></div><div class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></div><div class="line">      $(<span class="string">&apos;form&apos;</span>).on(<span class="string">&apos;submit&apos;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>{</div><div class="line">        e.preventDefault();</div><div class="line">        $.ajax({</div><div class="line">          <span class="attr">url</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;action&apos;</span>),</div><div class="line">          <span class="attr">type</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;method&apos;</span>),</div><div class="line">          <span class="attr">data</span>: $(<span class="keyword">this</span>).serialize(),</div><div class="line">          <span class="attr">dataType</span>: <span class="string">&apos;json&apos;</span></div><div class="line">        }).then(<span class="function"><span class="params">data</span> =&gt;</span> {</div><div class="line">          <span class="keyword">switch</span> (data.code) {</div><div class="line">            <span class="keyword">case</span> <span class="number">3000</span>:</div><div class="line">              <span class="built_in">window</span>.location.href = <span class="string">&apos;/&apos;</span></div><div class="line">              <span class="keyword">break</span>;</div><div class="line">            <span class="keyword">case</span> <span class="number">3001</span>:</div><div class="line">              <span class="built_in">window</span>.alert(<span class="string">&apos;&#x8BE5;&#x7528;&#x6237;&#x4E0D;&#x5B58;&#x5728;&apos;</span>);</div><div class="line">              <span class="keyword">break</span>;</div><div class="line">            <span class="keyword">case</span> <span class="number">3002</span>:</div><div class="line">              <span class="built_in">window</span>.alert(<span class="string">&apos;&#x5BC6;&#x7801;&#x9519;&#x8BEF;&#x4E86;&apos;</span>);</div><div class="line">              <span class="keyword">break</span>;</div><div class="line">            <span class="keyword">default</span>:</div><div class="line">              <span class="built_in">window</span>.alert(<span class="string">&apos;&#x672A;&#x77E5;&#x9519;&#x8BEF;&apos;</span>);</div><div class="line">              <span class="keyword">break</span>;</div><div class="line">          }</div><div class="line">        });</div><div class="line">      });</div><div class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h4 id="&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;"><a href="#&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;" class="headerlink" title="&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;"></a>&#x5904;&#x7406;&#x6CE8;&#x518C;&#x9875;&#x9762;</h4><blockquote>
<p>&#x6700;&#x7EC8;&#x7684; singup.html</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;utf-8&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>&#x7528;&#x6237;&#x767B;&#x9646;<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/node_modules/bootstrap/dist/css/bootstrap.css&quot;</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/public/css/signin.css&quot;</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;/signup&quot;</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-signin&quot;</span> <span class="attr">role</span>=<span class="string">&quot;form&quot;</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">h2</span> <span class="attr">class</span>=<span class="string">&quot;form-signin-heading&quot;</span>&gt;</span>&#x7528;&#x6237;&#x6CE8;&#x518C;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x7528;&#x6237;&#x540D;&quot;</span> <span class="attr">required</span> <span class="attr">autofocus</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;email&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">name</span>=<span class="string">&quot;email&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x90AE;&#x7BB1;&quot;</span> <span class="attr">required</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;password&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x5BC6;&#x7801;&quot;</span> <span class="attr">required</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;confirm&quot;</span> <span class="attr">class</span>=<span class="string">&quot;form-control&quot;</span> <span class="attr">name</span>=<span class="string">&quot;confirm&quot;</span> <span class="attr">placeholder</span>=<span class="string">&quot;&#x8BF7;&#x786E;&#x8BA4;&#x5BC6;&#x7801;&quot;</span> <span class="attr">required</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">button</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-lg btn-primary btn-block&quot;</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span>&gt;</span>&#x6CE8;&#x518C;<span class="tag">&lt;/<span class="name">button</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">form</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;/node_modules/jquery/dist/jquery.js&quot;</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="comment">// &#x5904;&#x7406;&#x524D;&#x53F0;&#x903B;&#x8F91;</span></div><div class="line">    $(<span class="string">&apos;form&apos;</span>).on(<span class="string">&apos;submit&apos;</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>{</div><div class="line">      e.preventDefault();</div><div class="line">      $.ajax({</div><div class="line">          <span class="attr">url</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;action&apos;</span>),</div><div class="line">          <span class="attr">type</span>: $(<span class="keyword">this</span>).attr(<span class="string">&apos;method&apos;</span>),</div><div class="line">          <span class="attr">data</span>: $(<span class="keyword">this</span>).serialize(),</div><div class="line">          <span class="attr">dataType</span>: <span class="string">&apos;json&apos;</span></div><div class="line">        })</div><div class="line">        .then(<span class="function"><span class="params">data</span> =&gt;</span> {</div><div class="line">          <span class="keyword">if</span> (data.code === <span class="number">2000</span>) {</div><div class="line">            <span class="built_in">window</span>.alert(<span class="string">&apos;&#x606D;&#x559C;&#xFF1A;&#x6CE8;&#x518C;&#x6210;&#x529F;&apos;</span>);</div><div class="line">            <span class="comment">// &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x63A7;&#x5236; location &#x7684; href &#xFF0C;&#x8BA9;&#x6D4F;&#x89C8;&#x5668;&#x8DF3;&#x8F6C;</span></div><div class="line">            <span class="built_in">window</span>.location.href = <span class="string">&apos;/&apos;</span>;</div><div class="line">          } <span class="keyword">else</span> <span class="keyword">if</span> (data.code === <span class="number">2001</span>) {</div><div class="line">            <span class="built_in">window</span>.alert(<span class="string">&apos;&#x7528;&#x6237;&#x540D;&#x5DF2;&#x5B58;&#x5728;&#xFF0C;&#x8BF7;&#x66F4;&#x6362;&#x91CD;&#x8BD5;&apos;</span>);</div><div class="line">          }</div><div class="line">        });</div><div class="line">    });</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h4 id="&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;"><a href="#&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;" class="headerlink" title="&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;"></a>&#x6570;&#x636E;&#x5E93;&#x8FDE;&#x63A5;</h4><blockquote>
<p>&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x5173;&#x952E;&#x4EE3;&#x7801;&#xFF1A;db.js</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> mysql = <span class="built_in">require</span>(<span class="string">&apos;mysql&apos;</span>);</div><div class="line"></div><div class="line"><span class="comment">// &#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#xFF0C;&#x63D0;&#x9AD8;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x6548;&#x7387;</span></div><div class="line"><span class="comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8FDE;&#x63A5;&#x6C60;&#xFF0C;&#x6C60;&#x5B50;&#x5B58;&#x653E;&#x7684;&#x8FDE;&#x63A5;&#x6570;&#x91CF;&#x662F; 100 &#x4E2A;</span></div><div class="line"><span class="keyword">const</span> pool = mysql.createPool({</div><div class="line">  <span class="attr">connectionLimit</span>: <span class="number">100</span>,</div><div class="line">  <span class="attr">host</span>: <span class="string">&apos;localhost&apos;</span>,</div><div class="line">  <span class="attr">user</span>: <span class="string">&apos;root&apos;</span>,</div><div class="line">  <span class="attr">password</span>: <span class="string">&apos;*********&apos;</span>,</div><div class="line">  <span class="attr">database</span>: <span class="string">&apos;feedback&apos;</span></div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// rest &#x53C2;&#x6570;</span></div><div class="line"><span class="comment">// &#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x51FA;&#x73B0;&#xFF0C;&#x4EE5; ... &#x5F00;&#x5934;&#xFF0C;&#x540E;&#x9762;&#x8DDF;&#x4E00;&#x4E2A;&#x540D;&#x5B57;</span></div><div class="line"><span class="comment">// rest &#x53C2;&#x6570;&#x5C31;&#x4EE3;&#x66FF;&#x4E86; arguments</span></div><div class="line">exports.query = <span class="function"><span class="keyword">function</span> (<span class="params">sql, ...values</span>) </span>{</div><div class="line">  <span class="keyword">let</span> callback;</div><div class="line">  <span class="keyword">let</span> params = [];</div><div class="line">  <span class="keyword">if</span> (values.length === <span class="number">3</span>) {</div><div class="line">    params = values[<span class="number">1</span>];</div><div class="line">    callback = values[<span class="number">2</span>];</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (values.length === <span class="number">2</span>) {</div><div class="line">    callback = values[<span class="number">1</span>];</div><div class="line">  }</div><div class="line">  pool.getConnection(<span class="function">(<span class="params">err, connection</span>) =&gt;</span> {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> callback(err);</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x5982;&#x679C;&#x4F20;&#x9012;&#x4E86;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x5C31;&#x662F; callback&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;params &#x5C31;&#x662F; callback</span></div><div class="line">    <span class="comment">// &#x540E;&#x9762;&#x7684; &#x53C2;&#x6570;&#x5C31;&#x5FFD;&#x7565;&#x4E0D;&#x8BA1;&#x4E86;</span></div><div class="line">    <span class="comment">// &#x5982;&#x679C;&#x4F20;&#x9012;&#x4E86;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x5C31;&#x662F;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;</span></div><div class="line">    connection.query(sql, params, (err, result) =&gt; {</div><div class="line">      connection.release();</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> callback(err);</div><div class="line">      }</div><div class="line">      callback(<span class="literal">null</span>, result);</div><div class="line">    })</div><div class="line">  });</div><div class="line">};</div></pre></td></tr></table></figure>
<h4 id="css"><a href="#css" class="headerlink" title="css"></a>css</h4><figure class="highlight css"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="selector-tag">body</span> { <span class="attribute">padding-top</span>: <span class="number">40px</span>; <span class="attribute">padding-bottom</span>: <span class="number">40px</span>; <span class="attribute">background-color</span>: <span class="number">#eee</span>; }</div><div class="line"><span class="selector-class">.form-signin</span> {<span class="attribute">max-width</span>: <span class="number">330px</span>; <span class="attribute">padding</span>: <span class="number">15px</span>; <span class="attribute">margin</span>: <span class="number">0</span> auto;}</div><div class="line"><span class="selector-class">.form-signin</span> <span class="selector-class">.form-signin-heading</span>,<span class="selector-class">.form-signin</span> <span class="selector-class">.checkbox</span> {<span class="attribute">margin-bottom</span>: <span class="number">10px</span>;}</div><div class="line"><span class="selector-class">.form-signin</span> <span class="selector-class">.checkbox</span> {<span class="attribute">font-weight</span>: normal;}</div><div class="line"><span class="selector-class">.form-signin</span> <span class="selector-class">.form-control</span> { <span class="attribute">position</span>: relative; <span class="attribute">height</span>: auto; <span class="attribute">box-sizing</span>: border-box; <span class="attribute">padding</span>: <span class="number">10px</span>; <span class="attribute">font-size</span>: <span class="number">16px</span>; }</div><div class="line"><span class="selector-class">.form-signin</span> <span class="selector-class">.form-control</span><span class="selector-pseudo">:focus</span> {<span class="attribute">z-index</span>: <span class="number">2</span>;}</div><div class="line"><span class="selector-class">.form-signin</span> <span class="selector-tag">input</span><span class="selector-attr">[type=&quot;email&quot;]</span> {<span class="attribute">margin-bottom</span>: -<span class="number">1px</span>; <span class="attribute">border-bottom-right-radius</span>: <span class="number">0</span>; <span class="attribute">border-bottom-left-radius</span>: <span class="number">0</span>;}</div><div class="line"><span class="selector-class">.form-signin</span> <span class="selector-tag">input</span><span class="selector-attr">[type=&quot;password&quot;]</span> { <span class="attribute">margin-bottom</span>: <span class="number">10px</span>; <span class="attribute">border-top-left-radius</span>: <span class="number">0</span>; <span class="attribute">border-top-right-radius</span>: <span class="number">0</span>; }</div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Node-js/" rel="tag">#Node.js</a>
          
            <a href="/tags/Express/" rel="tag">#Express</a>
          
            <a href="/tags/EJS/" rel="tag">#EJS</a>
          
            <a href="/tags/MySQL/" rel="tag">#MySQL</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/11/14/S013-Express中使用模板引擎/" rel="next" title="Express中使用模板引擎">
                <i class="fa fa-chevron-left"></i> Express中使用模板引擎
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/24/t12-混合APP开发环境配置/" rel="prev" title="混合APP开发环境配置-ionic、cordora">
                混合APP开发环境配置-ionic、cordora <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/11/19/s014-Node-留言列表小项目/"
     data-title="Node-留言列表小项目"
     data-content=""
     data-url="https://hongqinma.github.io/2016/11/19/s014-Node-留言列表小项目/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/11/19/s014-Node-留言列表小项目/"
           data-title="Node-留言列表小项目" data-url="https://hongqinma.github.io/2016/11/19/s014-Node-留言列表小项目/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#依赖"><span class="nav-number">1.</span> <span class="nav-text">依赖</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装依赖"><span class="nav-number">1.1.</span> <span class="nav-text">安装依赖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#package-json-包括项目后面流程安装的依赖包"><span class="nav-number">1.2.</span> <span class="nav-text">package.json (包括项目后面流程安装的依赖包)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#页面搭建：Bootstrap"><span class="nav-number">2.</span> <span class="nav-text">页面搭建：Bootstrap</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库设计-feedback"><span class="nav-number">3.</span> <span class="nav-text">数据库设计: feedback</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#页面入口-后台数据处理-app-js"><span class="nav-number">4.</span> <span class="nav-text">页面入口/后台数据处理 app.js</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#渲染页面-EJS"><span class="nav-number">5.</span> <span class="nav-text">渲染页面 EJS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#前台-ajax-请求"><span class="nav-number">6.</span> <span class="nav-text">前台 ajax 请求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#后台-post-请求处理"><span class="nav-number">7.</span> <span class="nav-text">后台 post 请求处理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#注册登录模块"><span class="nav-number">8.</span> <span class="nav-text">注册登录模块</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#登录页面"><span class="nav-number">8.1.</span> <span class="nav-text">登录页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#注册页面"><span class="nav-number">8.2.</span> <span class="nav-text">注册页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#流程"><span class="nav-number">8.3.</span> <span class="nav-text">流程</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#添加表-users"><span class="nav-number">8.3.1.</span> <span class="nav-text">添加表 users</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#处理注册页面后台逻辑"><span class="nav-number">8.3.2.</span> <span class="nav-text">处理注册页面后台逻辑</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#处理注册页面前台逻辑"><span class="nav-number">8.3.3.</span> <span class="nav-text">处理注册页面前台逻辑</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#记住用户名（通过前台来写）"><span class="nav-number">8.3.4.</span> <span class="nav-text">记住用户名（通过前台来写）</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#在-express-中使用-cookie-parser"><span class="nav-number">8.3.4.1.</span> <span class="nav-text">在 express 中使用 cookie-parser</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#在-express-中使用-session"><span class="nav-number">8.3.4.2.</span> <span class="nav-text">在 express 中使用 session</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#处理用户注册和登录的状态"><span class="nav-number">8.4.</span> <span class="nav-text">处理用户注册和登录的状态</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#最终的-app-js"><span class="nav-number">8.4.1.</span> <span class="nav-text">最终的 app.js</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#处理登录页面"><span class="nav-number">8.4.2.</span> <span class="nav-text">处理登录页面</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#处理注册页面"><span class="nav-number">8.4.3.</span> <span class="nav-text">处理注册页面</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据库连接"><span class="nav-number">8.4.4.</span> <span class="nav-text">数据库连接</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#css"><span class="nav-number">8.4.5.</span> <span class="nav-text">css</span></a></li></ol></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
